﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace QLTV_GiaoDien
{
    public partial class uc_NhomNguoiDung : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_NhomNguoiDung()
        {
            InitializeComponent();
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Text = string.Empty;
            }
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        public void enableTextFalse(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Enabled = false;
            }
            foreach (Control i in ctr.Controls)
                enableTextFalse(i);
        }

        NhomNguoiDung_BLL nhom_bll = new NhomNguoiDung_BLL();
        int flag;
        private void uc_NhomNguoiDung_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = nhom_bll.LayDuLieu();
            panelControl1.Hide();
            cob_MaQuyen.Hide();
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Xem.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
        }
        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 0;
            bbi_Them.Enabled = false;
            txt_MaNhom.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Xem.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            panelControl1.Show();
        }
        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 1;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Xem.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            txt_MaNhom.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Nhóm Người Dùng").ToString();
            tg.XXX= txt_TenNhom.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Nhóm").ToString();
            cob_QuyenHan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Quyền").ToString();
            txt_GhiChu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            panelControl1.Show();
        }
        private void bbi_Xem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 2;
            txt_MaNhom.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Nhóm Người Dùng").ToString();
            tg.XXX=txt_TenNhom.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Nhóm").ToString();
            cob_QuyenHan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Quyền").ToString();
            txt_GhiChu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            enableTextFalse(this);
            panelControl1.Show();
        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NhomNguoiDung_Public n = new NhomNguoiDung_Public();
            if (flag == 0)
            {
                //them
                n.TenNhom = txt_TenNhom.Text;
                n.MaQuyen = int.Parse(cob_MaQuyen.Text);
                n.GhiChu1 = txt_GhiChu.Text;
                nhom_bll.insert_NhomNguoiDung(n,n,n);
                // 
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Thêm";
                nk.MoTa = "Thêm Nhóm người dùng: '"+txt_TenNhom.Text+"'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (flag ==1)
            {
                //sua
                n.MaNhom = int.Parse(txt_MaNhom.Text);
                n.TenNhom = txt_TenNhom.Text;
                n.MaQuyen = int.Parse(cob_MaQuyen.Text);
                n.GhiChu1 = txt_GhiChu.Text;
                nhom_bll.update_NhomNguoiDung(n,n, n, n);
                // 
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Nhóm người dùng: '" + tg.XXX + "' thành: '"+txt_TenNhom.Text+"'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (flag == 2)
            {
                //xem
                n.MaNhom = int.Parse(txt_MaNhom.Text);
                n.TenNhom = txt_TenNhom.Text;
                n.MaQuyen = int.Parse(cob_MaQuyen.Text);
                n.GhiChu1 = txt_GhiChu.Text;
                nhom_bll.update_NhomNguoiDung(n, n, n, n);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Nhóm người dùng: '" + tg.XXX + "' thành: '" + txt_TenNhom.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            uc_NhomNguoiDung_Load(sender,e);
            Huy_Text(this);
            panelControl1.Show();
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Xem.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
        }
        private void bbi_Xoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NhomNguoiDung_Public n = new NhomNguoiDung_Public();
            n.MaNhom = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Nhóm Người Dùng").ToString());
            n.TenNhom =gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Nhóm").ToString();
            if (XtraMessageBox.Show("Bạn có muốn xóa Tên nhóm quyền: '" + n.TenNhom + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Xóa";
                nk.MoTa = "Xóa thông tin Nhóm người dùng: '" + n.TenNhom + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                nhom_bll.delete_NhomNguoiDung(n);
                uc_NhomNguoiDung_Load(sender,e);
            }
        }

        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);
            panelControl1.Hide();
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Xem.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
            
        }

        private void bbi_Refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            uc_NhomNguoiDung_Load(sender,e);
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            enableTextFalse(this);
            txt_MaNhom.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Nhóm Người Dùng").ToString();
            tg.XXX=txt_TenNhom.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Nhóm").ToString();
            cob_QuyenHan.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Quyền").ToString();
            txt_GhiChu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            panelControl1.Show();
        }

        private void cob_QuyenHan_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sql = "select * from tbl_QuyenHan";
            dt = nhom_bll.TaoBang(sql);
            cob_MaQuyen.DataSource = dt;
            cob_MaQuyen.DisplayMember = "MaQuyen";
            cob_QuyenHan.DataSource = dt;
            cob_QuyenHan.DisplayMember = "TenQuyen";
        }

        private void cob_QuyenHan_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sql = "select * from tbl_QuyenHan where tbl_QuyenHan.TenQuyen='" + cob_QuyenHan.Text + "'";
            dt = nhom_bll.TaoBang(sql);
            cob_MaQuyen.DataSource = dt;
            cob_MaQuyen.DisplayMember = "MaQuyen";
        }

        private void txt_MaNhom_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaNhom_Leave(object sender, EventArgs e)
        {
            if (txt_MaNhom.Text == "")
                txt_MaNhom.Focus();
        }

        private void cob_QuyenHan_Leave(object sender, EventArgs e)
        {
            if (cob_QuyenHan.Text == "")
                cob_QuyenHan.Focus();
        }

        private void txt_MaNhom_TextChanged(object sender, EventArgs e)
        {
            string sql = "select * from tbl_QuyenHan,tbl_NhomQuyen where tbl_QuyenHan.MaQuyen=tbl_NhomQuyen.MaQuyen and tbl_NhomQuyen.MaNhom='"+txt_MaNhom.Text+"'";
            DataTable dt=new DataTable();
            dt=nhom_bll.TaoBang(sql);
            cob_MaQuyen.DataSource=dt;
            cob_MaQuyen.DisplayMember="MaQuyen";
        }




    }
}
